package com.xebest.web.controller.order;  

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenOrderForm;
import com.xebest.web.service.XeVenPrintsService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenSendOrderPrintView;

@RestController
@RequestMapping("print")
public class XeVenPrintsController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(XeVenPrintsController.class);//日志记录类
	
	//打印服务类
	@Autowired
	private XeVenPrintsService printsService;
	
	/**
	 * 订单打印发货
	 * @author 赵盼飞
	 */
	@RequestMapping("querySendOrderPrintList")
	public @ResponseBody BaseResponse querySendOrderPrintList(@RequestBody XeVenOrderForm form){

		if (null == form.getOrderArr() || form.getOrderArr().length<=0) {
			return sendAjaxError(BASEMessage.PARAM_ERROR);
		}
		
		//查询列表
		ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
		log.info("订单打印发货 请求参数为：{}",JSON.toJSONString(form.getOrderArr()));
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("userCode", userInfo.getUserCode());
		paraMap.put("orderArr", form.getOrderArr());
		try {
			List<XeVenSendOrderPrintView> orderInfoList = this.printsService.querySendOrderPrintList(paraMap);
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("orderInfoList", orderInfoList);
			resultMap.put("venSimpleCode", userInfo.getVenSimpleCode()==null?"":userInfo.getVenSimpleCode().substring(0,userInfo.getVenSimpleCode().length()-1));
			return sendAjaxOK(resultMap);
		} catch (Exception e) {
			log.error("查询订单打印发货 接口异常：{}" , e);
			return sendAjaxError();
		}
	}
	
	
	/**
	 * 订单打印发货
	 * @author 赵盼飞
	 */
	@RequestMapping("updateOrderPrintState")
	public @ResponseBody BaseResponse updateOrderPrintState(@RequestBody XeVenOrderForm form){
		try {
			if (null == form.getPrintParaList()) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
		    log.info("更新订单打印状态参数：{}",JSON.toJSONString(form.getPrintParaList()));
			int result = printsService.batchUpdateOrderPrintStauts(form.getPrintParaList());
			log.info("更新订单打印状态成功执行：{}条",result);
		    return sendAjaxOK();
		} catch (Exception e) {
			log.error("打印订单修改打印状态 接口异常：{}" , e);
		}
		return sendAjaxError();
	}
	
}